﻿namespace Sedodream.Web.Common.View.HtmlView
{
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    // TODO: Rename the file to match casing
public class HtmlFieldset : AbstractHtmlViewObject
{
    private readonly string mTitle;

    public HtmlFieldset(
        IViewRequestContext requestContext, string name, 
        string title, object attributes)
        : base(requestContext, name)
    {
        mTitle = title;
        Attributes = attributes;
    }

    private TagBuilder FieldsetTag { get; set; }

    private TagBuilder OlTag { get; set; }

    public override void StartView()
    {
        HttpResponseBase httpResponse = RequestContext.HttpResponse;

        FieldsetTag = new TagBuilder("fieldset");

        // apply any Attributes passed in
        if (Attributes != null)
        {
            FieldsetTag.MergeAttributes(new RouteValueDictionary(Attributes));
        }

        // The Name property should override any passed into the Attributes
        FieldsetTag.MergeAttribute("name", Name, true);

        httpResponse.Write(FieldsetTag.ToString(TagRenderMode.StartTag));

        if (!string.IsNullOrEmpty(mTitle))
        {
            TagBuilder legendTag = new TagBuilder("legend");
            legendTag.SetInnerText(mTitle);
            httpResponse.Write(legendTag.ToString(TagRenderMode.Normal));
        }

        OlTag = new TagBuilder("ol");
        httpResponse.Write(OlTag.ToString(TagRenderMode.StartTag));
    }

    public override void EndView()
    {
        HttpResponseBase httpResponse = RequestContext.HttpResponse;

        httpResponse.Write(OlTag.ToString(TagRenderMode.EndTag));
        httpResponse.Write(FieldsetTag.ToString(TagRenderMode.EndTag));
    }
}
}